package cn.stylefeng.guns.modular.tool.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.justApi;
import cn.stylefeng.guns.core.util.justGetXSCKDApi;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.tool.entity.ToolWave;
import cn.stylefeng.guns.modular.tool.model.params.ToolBasicPackInstallParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressExcelParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressScanParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressSumParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressSumTaskParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolGoodsInstallParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolTaskArrangeParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolTaskMoneyParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolWaveParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolWorkParam;
import cn.stylefeng.guns.modular.tool.model.result.ToolBasicPackInstallResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolExpressExcelResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolExpressResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolGoodsInstallResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolTaskArrangeResult;
import cn.stylefeng.guns.modular.tool.service.ToolBasicPackInstallService;
import cn.stylefeng.guns.modular.tool.service.ToolExpressExcelService;
import cn.stylefeng.guns.modular.tool.service.ToolExpressScanService;
import cn.stylefeng.guns.modular.tool.service.ToolExpressService;
import cn.stylefeng.guns.modular.tool.service.ToolExpressSumService;
import cn.stylefeng.guns.modular.tool.service.ToolExpressSumTaskService;
import cn.stylefeng.guns.modular.tool.service.ToolGoodsInstallService;
import cn.stylefeng.guns.modular.tool.service.ToolTaskArrangeService;
import cn.stylefeng.guns.modular.tool.service.ToolTaskMoneyService;
import cn.stylefeng.guns.modular.tool.service.ToolWaveService;
import cn.stylefeng.guns.modular.tool.service.ToolWorkService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 统计波次控制器
 */
@Controller
@RequestMapping("/toolWave")
public class ToolWaveController extends BaseController {

    private String PREFIX = "/tool/toolWave";

    @Autowired
    private ToolWaveService toolWaveService;

    @Autowired
    private BasicCustomerService basicCustomerService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @Autowired
    private ToolBasicPackInstallService toolBasicPackInstallService;
    
    @Autowired
    private UserService userService;
    
    private Lock lock = new ReentrantLock();

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/toolWave.html";
    }

    /**
     * 选择小组页面
     */
    @RequestMapping("/toselectArrange")
    public String toselectArrange() {
        return PREFIX + "/select_arrange.html";
    }

    /**
     * 选择小组页面
     */
    @RequestMapping("/toselectArrange2")
    public String toselectArrange2() {
        return PREFIX + "/select_arrange2.html";
    }

    /**
     * 分配人员页面
     */
    @RequestMapping("/choice_user")
    public String choice_user(Model model,Long waveId) {
    	ToolTaskArrangeParam toolTaskArrangeParam = new ToolTaskArrangeParam();
    	toolTaskArrangeParam.setWaveId(waveId);
    	List<ToolTaskArrangeResult> alist = toolTaskArrangeService.findListBySpec(toolTaskArrangeParam);
    	model.addAttribute("alist", alist);
        return PREFIX + "/choice_user.html";
    }
    
    /**
     * 跳转到更新库存页面
     */
    @RequestMapping("toupdateStock")
    public String toupdateStock(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择仓库
    	model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/updateStock.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/toolWave_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/toolWave_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ToolWaveParam toolWaveParam) {
    	User user = userService.getcurrentUser();
    	toolWaveParam.setDeptId(user.getDeptId());
    	toolWaveParam.setCreateUserName(user.getName());
        this.toolWaveService.add(toolWaveParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ToolWaveParam toolWaveParam) {
        ToolWave detail = this.toolWaveService.getById(toolWaveParam.getId());
        if(!toolWaveParam.getWaveName().equals(detail.getWaveName())) {
        	ToolTaskMoneyParam moneyParam = new ToolTaskMoneyParam();
        	moneyParam.setWaveId(Long.parseLong(detail.getId().toString()));
        	moneyParam.setV1(toolWaveParam.getWaveName());
        	toolTaskMoneyService.updateBySpec(moneyParam);
        }
        this.toolWaveService.update(toolWaveParam);
        return ResponseData.success();
    }

    @Autowired
    private ToolExpressService toolExpressService;

    @Autowired
    private ToolExpressSumService toolExpressSumService;

    @Autowired
    private ToolExpressSumTaskService toolExpressSumTaskService;
    
    @Autowired
    private ToolExpressExcelService toolExpressExcelService;

    @Autowired
    private ToolExpressScanService toolExpressScanService;

    @Autowired
    private ToolTaskMoneyService toolTaskMoneyService;

    @Autowired
    private ToolTaskArrangeService toolTaskArrangeService;

    @Autowired
    private ToolWorkService toolWorkService;
    
    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ToolWaveParam toolWaveParam) {
        this.toolWaveService.delete(toolWaveParam);

        ToolExpressExcelParam toolExpressExcelParam = new ToolExpressExcelParam();
        toolExpressExcelParam.setWaveId(Long.parseLong(toolWaveParam.getId().toString()));
        toolExpressExcelService.deleteBySpec(toolExpressExcelParam);
        
        ToolExpressParam expressParam = new ToolExpressParam();
        expressParam.setWaveId(Long.parseLong(toolWaveParam.getId().toString()));
        toolExpressService.deleteBySpec(expressParam);

        ToolExpressSumParam expressSumParam = new ToolExpressSumParam();
        expressSumParam.setWaveId(Long.parseLong(toolWaveParam.getId().toString()));
        toolExpressSumService.deleteBySpec(expressSumParam);
        
        ToolTaskArrangeParam toolTaskArrangeParam = new ToolTaskArrangeParam();
        toolTaskArrangeParam.setWaveId(Long.parseLong(toolWaveParam.getId().toString()));
    	toolTaskArrangeService.deleteBySpec(toolTaskArrangeParam);
        
        ToolExpressScanParam scanParam = new ToolExpressScanParam();
        scanParam.setWaveId(Long.parseLong(toolWaveParam.getId().toString()));
        toolExpressScanService.deleteBySpec(scanParam);
        
        ToolWorkParam toolWorkParam = new ToolWorkParam();
        toolWorkParam.setWaveId(Long.parseLong(toolWaveParam.getId().toString()));
        toolWorkService.deleteBySpec(toolWorkParam);
        
        ToolExpressSumTaskParam expressSumTaskParam = new ToolExpressSumTaskParam();
        expressSumTaskParam.setWaveId(Long.parseLong(toolWaveParam.getId().toString()));
        toolExpressSumTaskService.deleteBySpec(expressSumTaskParam);

    	ToolTaskMoneyParam moneyParam = new ToolTaskMoneyParam();
    	moneyParam.setWaveId(Long.parseLong(toolWaveParam.getId().toString()));
    	toolTaskMoneyService.deleteBySpec(moneyParam);//删除工资明细
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ToolWaveParam toolWaveParam) {
        ToolWave detail = this.toolWaveService.getById(toolWaveParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ToolWaveParam toolWaveParam,String condition ) {
    	User user = userService.getcurrentUser();
    	toolWaveParam.setDeptId(user.getDeptId());
        return this.toolWaveService.findPageBySpec(toolWaveParam,condition,user.getOperateId());
    }

}


